package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.TFrontUser;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import annotation.Log;
import persistent.factory.DAOFactory;
import util.toolkit.Constant;
import util.toolkit.Expression;
import util.toolkit.ResponseJSON;
import util.toolkit.TimeUtil;

/**
 * 网站前端用户管理控制类
 * @author djf
 * @since 2018/12/12
 */
@Controller
@RequestMapping(value="/syspublicusermanagement")
public class SysPublicUserManagementController {

	/**
	 * 添加一个前端用户信息
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 * @author djf
	 */
	//TODO 添加一个前端用户信息
	@Log(operationType = Constant.OPERTATION_ADD, operationDescribe = "添加一个前端用户信息", 
			operationClassPath = "controller.SysTitleManagementController.addPublicUser(Model model, HttpServletRequest req,HttpServletResponse resp)", 
			operationMenu = "网站用户管理/用户信息添加")
	@RequestMapping(value="/addpublicuser")
	public String addPublicUser(Model model,HttpServletRequest req,HttpServletResponse resp) throws IOException{
		
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");
		
		JSONObject result=new JSONObject();
		TFrontUser frontuser=new TFrontUser();
		TimeUtil time=new TimeUtil();
		
		try {
			String username=req.getParameter("username");
			String realname=req.getParameter("realname");
			String sex=req.getParameter("sex");
			String phone=req.getParameter("phone");
			String birthday=req.getParameter("birthday");
			
			if(username!=null && !username.equals("") && phone!=null && !phone.equals("")){
				frontuser.setUsername(username);
				frontuser.setPwd("000000");
				frontuser.setRealname(realname);
				frontuser.setSex(sex);
				frontuser.setPhone(phone);
				frontuser.setBirthday(birthday);
				frontuser.setCreatetime(time.toTimestamp(new Date()).toString());
				
				boolean row=DAOFactory.getFrontUserDAO().addFrontUser(frontuser);
				if(row){
					result.put("code", ResponseJSON.FLAG_SUCC);
					result.put("msg", "网站用户添加成功！");
				}else{
					result.put("code", ResponseJSON.FLAG_FAIL);
					result.put("msg", "你输入的用户名可能已经存在！");
				}
			}else{
				result.put("code", ResponseJSON.FLAG_FAIL);
				result.put("msg", "网站用户添加失败！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", ResponseJSON.FLAG_UNKNOWN_ERORR);
			result.put("msg", "网站用户添加失败！");
		}
		
		
		PrintWriter out=resp.getWriter();
		out.println(result);
		return null;
	}
	
	/**
	 * 修改网站用户信息
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 * @author djf
	 */
	//TODO 修改网站用户信息
	@Log(operationType = Constant.OPERTATION_UPDATE, operationDescribe = "修改一个前端用户信息", 
			operationClassPath = "controller.SysTitleManagementController.updatePublicUser(Model model, HttpServletRequest req,HttpServletResponse resp)", 
			operationMenu = "网站用户管理/用户信息查看")
	@RequestMapping(value="/updatepublicuser")
	public String updatePublicUser(Model model,HttpServletRequest req,HttpServletResponse resp) throws IOException{
	
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");
		
		JSONObject result=new JSONObject();
		
		try {
			String username=req.getParameter("username");
			String sex=req.getParameter("sex");
			String realname=req.getParameter("realname");
			String phone=req.getParameter("phone");
			String birthday=req.getParameter("birthday");
			if(phone!=null && !phone.equals("")){
				TFrontUser frontuser=DAOFactory.getFrontUserDAO().getATFrontUser(username);
				frontuser.setSex(sex);
				frontuser.setRealname(realname);
				frontuser.setPhone(phone);
				frontuser.setBirthday(birthday);
				
				boolean row=DAOFactory.getFrontUserDAO().updateFrontUser(frontuser);
				if(row){
					result.put("code", ResponseJSON.FLAG_SUCC);
					result.put("msg", "网站用户修改成功！");
				}else{
					result.put("code", ResponseJSON.FLAG_FAIL);
					result.put("msg", "网站用户修改失败！");
				}
			}else{
				result.put("code", ResponseJSON.FLAG_FAIL);
				result.put("msg", "网站用户修改失败！");
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", ResponseJSON.FLAG_UNKNOWN_ERORR);
			result.put("msg", "网站用户修改失败！");
		}
		
		
		PrintWriter out=resp.getWriter();
		out.println(result);
		return null;
	}
	
	/**
	 * 根据条件查询出用户信息
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 * @author djf
	 */
	//TODO 根据条件查询出用户信息
	@Log(operationType = Constant.OPERTATION_QUERY, operationDescribe = "根据条件查询出用户信息", 
			operationClassPath = "controller.SysTitleManagementController.getPublicUserList(Model model, HttpServletRequest req,HttpServletResponse resp)", 
			operationMenu = "网站用户管理/用户信息查看")
	@RequestMapping(value="/getpublicuserlist")
	public String getPublicUserList(Model model,HttpServletRequest req,HttpServletResponse resp) throws IOException{
		
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");
		
		JSONObject result=new JSONObject();
		Expression exp=new Expression();
		
		try {
			String username=req.getParameter("username");
			String currentPage=req.getParameter("page");
			String pageSize=req.getParameter("limit");
			if(username!=null && !username.equals("")){
				exp.andLeftBraLike("username", username, String.class);
				exp.orRightBraLike("realname", username, String.class);
			}
			exp.orderByDesc("createtime");
			List<TFrontUser> list=DAOFactory.getFrontUserDAO().getFrontUserList(
					exp.toString(), Integer.parseInt(currentPage), Integer.parseInt(pageSize));
			List<TFrontUser> listsize=DAOFactory.getFrontUserDAO().getFrontUserList(exp.toString());
			if(list!=null && list.size()>0 && listsize!=null && listsize.size()>0){
				result.put("code", 0);
				result.put("msg", "查询成功！");
				result.put("count", listsize.size());
				JSONArray jsonArray = JSONArray.fromObject(list);
				result.element("data", jsonArray);
			}else{
				result.put("code", 1);
				result.put("msg", "无查询结果！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", 2);
			result.put("msg", "无查询结果！");
		}
		
		PrintWriter out=resp.getWriter();
		out.println(result);
		return null;
	}
	
	/**
	 * 删除一个网站前端用户信息
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	//TODO 删除一个网站前端用户信息
	@Log(operationType = Constant.OPERTATION_DELETE, operationDescribe = "删除一个网站前端用户信息", 
			operationClassPath = "controller.SysTitleManagementController.deletePublicUser(Model model, HttpServletRequest req,HttpServletResponse resp)", 
			operationMenu = "网站用户管理/用户信息查看")
	@RequestMapping(value="/deletepublicuser")
	public String deletePublicUser(Model model,HttpServletRequest req,HttpServletResponse resp) throws IOException{
		
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");
		
		JSONObject result=new JSONObject();
		
		try {
			String username=req.getParameter("username");

			if(username!=null && !username.equals("")){

				boolean row=DAOFactory.getFrontUserDAO().deleteFrontUser(username);
				if(row){
					result.put("code", ResponseJSON.FLAG_SUCC);
					result.put("msg", "网站用户删除成功！");
				}else{
					result.put("code", ResponseJSON.FLAG_FAIL);
					result.put("msg", "该用户可能存在外键引用，删除失败");
				}
			}else{
				result.put("code", ResponseJSON.FLAG_FAIL);
				result.put("msg", "网站用户删除失败！");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", ResponseJSON.FLAG_UNKNOWN_ERORR);
			result.put("msg", "网站用户删除失败！");
		}
		
		PrintWriter out=resp.getWriter();
		out.println(result);
		return null;
	}
	
}
